/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.blog.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;

import com.altuure.yagdao.blog.dao.*;
import com.altuure.yagdao.blog.model.User;
import org.springframework.transaction.annotation.Transactional;

import com.altuure.yagdao.blog.model.Category;
import com.altuure.yagdao.blog.model.Post;
import com.altuure.yagdao.blog.model.Role;
import com.altuure.yagdao.blog.service.DataPopulator;

import de.svenjacobs.loremipsum.LoremIpsum;



/**populates data with sample data
 * @author altuure
 *
 */
   @Transactional
public class DataPopulatorImpl implements DataPopulator{
    private static final long YEAR_TIME=1000L*60L*60L*24L*365L;
    private BlogPropertyDAO blogDAO;
    private PostDAO postDAO;
    private TagDAO tagDAO;
    private LinkDAO linkDAO;
    private CategoryDAO categoryDAO;
    private UserDAO userDAO;
    private RoleDAO roleDAO;

    @Transactional
    public void populate() {
        populateAuth();
        populateLinks();
        populateBlogDetails();
        populatePosts();

    }

    private void populateAuth() {
       Role adminRole= roleDAO.create("ROLE_SUPERVISOR");
       Role managerRole= roleDAO.create("ROLE_MANAGER");
       Role editorRole= roleDAO.create("ROLE_EDITOR");
        final User user1 = userDAO.create("admin", "admin", "admin@demo.com");
       final User user2=userDAO.create("manager","manager","user@demo.com");
       final User user3=userDAO.create("user","user","user@demo.com");
        userDAO.updateRoles(user1.getId(), new HashSet(Arrays.asList(new Object[]{adminRole,editorRole})));
        userDAO.updateRoles(user2.getId(), new HashSet(Arrays.asList(new Object[]{managerRole,editorRole})));
        userDAO.updateRoles(user3.getId(), new HashSet(Arrays.asList(new Object[]{editorRole})));
    }

    private void populatePosts() {
        LoremIpsum loremIpsum = new LoremIpsum();
        ArrayList<Category> categories = new ArrayList<Category>();
        for (int i = 0; i < 5; i++) {
            String category = loremIpsum.getWords(1,i);
            Category c=categoryDAO.create(category);
            categories.add(c);
        }
        int categorySize = categories.size();
        for (int i = 0; i < 100; i++) {
            Category category=categories.get((int)Math.floor(Math.random()*categorySize));
            Date publishDate=new Date(System.currentTimeMillis()-(long)(Math.random()*(double)YEAR_TIME));
            String title = loremIpsum.getWords(3,(i*3)%50);

            String body = loremIpsum.getParagraphs((int)Math.floor(Math.random()*6)+1);
            Post p=postDAO.create(title,body,publishDate,category);
            for (int j = 0; j < 5; j++) {
                tagDAO.create(p,loremIpsum.getWords(1,(int)Math.floor(Math.random()*50)));
            }
        }
           
        
    }

    private void populateBlogDetails() {
      blogDAO.create("title", "Yet Another Blog");
      blogDAO.create("description", "Yet Another Blog");
      blogDAO.create("href", "http://www.altuure.com/projects/yablog");
      blogDAO.create("rsssize", "3");
      blogDAO.create("tagsize", "5");
        
    }

    /**
     * 
     */
    public void populateLinks() {
        int i=1;
       linkDAO.create("http://www.altuure.com","Altuure Blog",i++);
       linkDAO.create("http://www.dzone.com","DZone",i++);
       linkDAO.create("http://www.reddit.com","reddit",i++);
       linkDAO.create("http://www.bbc.com","BBC",i++);
       linkDAO.create("http://www.google.com","google",i++);
    }

    /**
     * @param blogDAO the blogDAO to set
     */
    public void setBlogDAO(BlogPropertyDAO blogDAO) {
        this.blogDAO = blogDAO;
    }

    /**
     * @param postDAO the postDAO to set
     */
    public void setPostDAO(PostDAO postDAO) {
        this.postDAO = postDAO;
    }

    /**
     * @param tagDAO the tagDAO to set
     */
    public void setTagDAO(TagDAO tagDAO) {
        this.tagDAO = tagDAO;
    }

    /**
     * @param linkDAO the linkDAO to set
     */
    public void setLinkDAO(LinkDAO linkDAO) {
        this.linkDAO = linkDAO;
    }

    /**
     * @param categoryDAO the categoryDAO to set
     */
    public void setCategoryDAO(CategoryDAO categoryDAO) {
        this.categoryDAO = categoryDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setRoleDAO(RoleDAO roleDAO) {
        this.roleDAO = roleDAO;
    }
}
